﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.ListViewItem;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Button = System.Windows.Forms.Button;

namespace WindowsFormsApp_4._8
{
    public partial class Form1 : Form
    {
        DetailForm detailForm;

        int minute;

        int second;

        int defaultNameListWidth;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            detailForm = new DetailForm();

            toolStripStatusLabel1.Text = "就绪";

            minute = DateTime.Now.Minute;
            second = DateTime.Now.Second;

            timer1.Start();

            defaultNameListWidth = this.nameList.Width;

            // 模拟从远程加载数据
            foreach (string drive in Environment.GetLogicalDrives())
            {
                /*

                 TreeNode treeNode = new TreeNode("部门",new TreeNode[] { 
                                new TreeNode("开发",new TreeNode[]{ 
                                    new TreeNode("java"),
                                    new TreeNode("c#"),
                                    new TreeNode("javascript")
                                }),
                                new TreeNode("财务"),
                                new TreeNode("CEO")
                            });
                 */
                var treeNode = new TreeNode(drive);
                treeNode.Tag = drive;
                treeView1.Nodes.Add(treeNode);
            }


            listView1.Columns.Add("文件名");
            listView1.Columns.Add("日期");
            listView1.Columns.Add("详情");
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtName.Text))
            {
                nameList.Items.Add(txtName.Text);
            }
            else
            {
                MessageBox.Show("输入的名字不能为空");
            }
        }

        private void clearList_Click(object sender, EventArgs e)
        {
            nameList.Items.Clear();
        }

        private void openForm2_Click(object sender, EventArgs e)
        {
            detailForm.ShowDialog(this);
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 因为使用了托盘图标，用户点击关闭的时候
                // 只需要把这个窗口给隐藏就行了
                e.Cancel = true;

                notifyIcon1.BalloonTipTitle = "注意";
                notifyIcon1.BalloonTipText = "再次单击我打开程序";
                notifyIcon1.ShowBalloonTip(1000);

                this.Hide();
            }
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            labelMouseLocation.Text = $"{e.Location.X},{e.Location.Y}";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            second++;
            if (second == 60)
            {
                second = 0;
                minute++;
            }

            labelTime.Text = $"{minute}:{second}";
        }

        public void setSize(int size)
        {
            this.Font = new Font(
                this.Font.Name.ToString(),
                size
            );
            //this.nameList.Width = defaultNameListWidth+ size;
        }

        public void setFont(string fontName)
        {
            this.Font = new Font(fontName, this.Font.Size);
        }

      


        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            listView1.Items.Clear();

            string fullPath = buildFullPathBasedOnParent(e.Node);

            loadSubDirectories(fullPath, e.Node);

            if (!e.Node.IsExpanded)
            {
                e.Node.ExpandAll();
            }

            foreach (string filePath in Directory.GetFiles(fullPath))
            {
                FileInfo fileInfo = new FileInfo(filePath);


                ListViewItem listViewItem = new ListViewItem(fileInfo.Name);
                listViewItem.SubItems.Add(new ListViewSubItem(listViewItem, fileInfo.CreationTime.ToString()));
                listViewItem.SubItems.Add(new ListViewSubItem(listViewItem, fileInfo.GetType().ToString()));

                listView1.Items.Add(listViewItem);
            }
        }

        /**
         * 为当前节点的所有子节点加载子节点
         */
        private static void loadSubDirectories(string fullPath, TreeNode node)
        {
            if (node.Nodes.Count == 0)
            {
                foreach (string subdirectoryFullPath in Directory.GetDirectories(fullPath))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(subdirectoryFullPath);
                    var treeNode = new TreeNode(directoryInfo.Name);
                    treeNode.Tag = directoryInfo.Name;
                    node.Nodes.Add(treeNode);
                }
            }
        }

        private string buildFullPathBasedOnParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                string parentPath = buildFullPathBasedOnParent(node.Parent);
                return parentPath + "\\" + node.Tag.ToString();
            }
            else
            {
                return node.Tag.ToString();
            }
        }
        
        /**
         * 因为这个方法定义在窗口(form)上
         * 所以只有针对窗口的事件才会被捕获
         * 针对控件（按钮等）的事件不会在这里被捕获
         * 捕获事件
         * https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.forms.control.wndproc?view=netframework-4.8
         */
        protected override void WndProc(ref Message m)
        {
            // https://wiki.winehq.org/List_Of_Windows_Messages
            if (m.Msg == 513)
            {
                // 鼠标左键被按下
                label1.Text = "鼠标左键被按下啦";
            }else if (m.Msg==516)
            {
                label1.Text = "右键";
            }
            else
            {
                base.WndProc(ref m);
            }

        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
    }


    
}